package com.easyssh.framework.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.easyssh.framework.common.utils.StringUtil;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @ClassName: GenericAction
 * @Description: TODO(GenericAction Action基类)
 * @author liangyuanbing
 * @date 2015年12月18日 下午5:57:13
 *
 */
public class GenericAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	
	public static final String LOGIN_INFO="login_info";
	public static final String ADMIN_INFO="admin_info";
	
	private String jsonText;
	
	private String contextPath;
	
	private String realPath;

	private long actionExecTime = 0L; // action执行时间
	
	public HttpServletRequest request=getRequest();
	
	public HttpServletResponse response=getResponse();
	
	/**
	 * 获得请求
	 * @return HttpServletRequest对象
	 */
	public HttpServletRequest getRequest(){
		return ServletActionContext.getRequest();
	}
	
	/**
	 * 获得请求响应
	 * @return HttpServletResponse对象
	 */
	public HttpServletResponse getResponse() {
		 return ServletActionContext.getResponse();  
	}
	
	/**
	 * 转换字符为utf-8编码
	 * @param s
	 * @return
	 */
	public String toUtf8Encode(String str) {
		if(StringUtils.isBlank(str)){
			return null;
		}
		return StringUtil.toUtf8Encode(str);
	}
	
	public String toUtf8Decode(String str){
		if(StringUtils.isBlank(str)){
			return null;
		}
		return StringUtil.toUtf8Decode(str);
	}
	/**
	 * 获取会话
	 * @return HttpSession对象
	 */
	public HttpSession getSession(){
		return ServletActionContext.getRequest().getSession();
	}
	
	/**
	 * 将值存入会话中
	 * @param key
	 * @param value
	 */
	public void setSessionAttribute(String key,Object value){
		this.getSession().setAttribute(key, value);
	}
	
	/**
	 * 从会话中取值
	 * @param key
	 * @return
	 */
	public Object getSessionAttribute(String key){
		return this.getSession().getAttribute(key);
	}
	
	/**
	 * 将前端用户登录对象存入会话
	 * @param value
	 */
	public void setLoginInfo(Object value){
		this.setSessionAttribute(LOGIN_INFO, value);
	}
	
	/**
	 * 获会话中的前端用户登录对象
	 * @return
	 */
	public Object getLoginInfo(){
		return this.getSessionAttribute(LOGIN_INFO);
	}
	
	/**
	 * 将管理员登录对象存入会话
	 * @param value
	 */
	public void setAdminInfo(Object value){
		this.setSessionAttribute(ADMIN_INFO, value);
	}
	
	/**
	 * 获管理员会话中的登录对象
	 * @return
	 */
	public Object getAdminInfo(){
		return this.getSessionAttribute(ADMIN_INFO);
	}
	
	/**
	 * @return json数据信息
	 */
	public String getJsonText() {
		return jsonText;
	}

	/**
	 * @param jsonMsg json数据信息
	 */
	public void setJsonText(String jsonText) {
		this.jsonText = jsonText;
	}
	
	/**
	 * @return
	 */
	public String getContextPath() {
		if(null==contextPath){
			contextPath=this.getContextPath(ServletActionContext.getRequest());
		}
		return this.contextPath;
	}
	
	/**
	 * @param request
	 * @return
	 */
	public String getContextPath(HttpServletRequest request) {
		return request == null ? "" : request.getContextPath();
	}

	/**
	 * @param contextPath
	 */
	public void setContextPath(String contextPath) {
		this.contextPath = contextPath;
	}
	
	public String getRealPath(){
		if(null==this.realPath){
			this.realPath=ServletActionContext.getServletContext().getRealPath("/");
		}
		return this.realPath;
	}
	
	public String getRealPath(String realPath){
		if(null==this.realPath){
			this.realPath=ServletActionContext.getServletContext().getRealPath(realPath);
		}
		return this.realPath;
	}
	
	public void setRealPath(String realPath){
		this.realPath=realPath;
	}

	/**
	 * @return action执行时间
	 */
	public long getActionExecTime() {
		return System.currentTimeMillis()-actionExecTime;
	}

	/**
	 * @param actionExecTime action执行时间
	 */
	public void setActionExecTime(long actionExecTime) {
		this.actionExecTime = actionExecTime;
	}
}
